package com.src.myapplication.ui.view.log.model;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import cn.bmob.sms.BmobSMS;
import cn.bmob.sms.exception.BmobException;
import cn.bmob.sms.listener.RequestSMSCodeListener;
import cn.bmob.sms.listener.VerifySMSCodeListener;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

/**
 * 类描述:
 * 创建人:一一哥
 * 创建时间:16/10/8 15:57
 * 备注:登录的具体业务逻辑实现的地方.
 */

public class LoginBiz implements IUserLoginBiz {

    @Override
    public void login(final Context context,final String name) {
        //登录过程一般需要联网.--->耗时.
        new Thread() {
            @Override
            public void run() {
                //模拟耗时操作....
                BmobSMS.requestSMSCode(context, name, "模板名称",new RequestSMSCodeListener() {

                    @Override
                    public void done(Integer smsId,BmobException ex) {
                        if(ex==null){//验证码发送成功
                            Toast.makeText(context,"验证码已发送",Toast.LENGTH_LONG).show();
                            Log.i("bmob", "短信id："+smsId);//用于查询本次短信发送详情
                        }
                    }
                });


            }
        }.start();

    }
    public void yanzheng(final Context context,final String name, final String pwd, final OnLoginListener listener) {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    BmobSMS.verifySmsCode(context,name, pwd, new VerifySMSCodeListener() {

                                        @Override
                                        public void done(BmobException ex) {
                                            if(ex==null) {//短信验证码已验证成功
                                                listener.add(true);
                                                BmobUser user = new BmobUser();
                                                user.setUsername(name);
                                                user.signUp(context, new SaveListener() {
                                                    @Override
                                                    public void onSuccess() {
                                                        Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();
                                                    }

                                                    @Override
                                                    public void onFailure(int i, String s) {
                                                        Toast.makeText(context, "注册失败", Toast.LENGTH_SHORT).show();
                                                    }
                                                });
                                            }else{
                                                Log.i("bmob", "验证失败：code ="+ex.getErrorCode()+",msg = "+ex.getLocalizedMessage());
                                                listener.add(false);
                                            }
                                        }
                                    });

                                }
                            }).start();
    }
}
